package javaCode.chapter14.generic;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class Generic01 {
    public static void main(String[] args) {

        //使用传统的方式来解决
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog("小白",1));
        arrayList.add(new Dog("小灰",2));
        arrayList.add(new Dog("小红",1));

        //加入我们的程序员，不小心，添加了一只猫
        arrayList.add(new Cat("小小",3));

        //遍历
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Dog dog = (Dog) iterator.next();
            System.out.println(String.format("name:%s, age:%d",dog.getName(),dog.getAge()));
        }
    }
}
/*
请编写程序，在ArrayList 中，添加3个Dog对象
Dog对象含有name 和 age，并输出name 和 age(要求使用getXxx())
 */
class Dog{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat{
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}